package com.hihiwjc.app.zzmh.adapters.home;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.hihiwjc.app.zzmh.R;
import com.hihiwjc.app.zzmh.entities.home.Recommend;
import com.hihiwjc.app.zzmh.views.AutoScrollViewPager;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * <br/>Created by hihiwjc on 2015/10/26 0026
 * <br/>Author:hihiwjc
 * <br/>Email:hihiwjc@live.com
 */

public class RecommendListAdapter extends BaseAdapter implements View.OnClickListener {
    private Context mContext;
    private List<Recommend.BannersEntity> mBanners;
    private List<Recommend.ListsEntity> mList;
    public static final int TYPE_BANNER = 0;
    public static final int TYPE_LIST_1_ITEM = 1;//横的长方形：每日追追资讯、每日漫评精选、二次元科普辞典、黑白漫文化专栏、漫画新作安利、热门话题讨论区、国漫牛逼了你造吗
    public static final int TYPE_LIST_2_ITEM = 2;//竖的长方形: 每日漫画推荐、国漫热门连载、少女爱情漫热门连载、少年热血漫热门连载
    public static final int TYPE_LIST_3_ITEM = 3;//精选专辑收录
    private final LayoutInflater mLayoutInflater;

    public RecommendListAdapter(Context context, @NonNull Recommend datas) {
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mBanners = datas.getBanners();
        mList = datas.getLists();
    }

    @Override
    public int getCount() {
        return mList.size() + (mBanners == null ? 0 : 1);
        //return 1;
    }

    @Override
    public int getItemViewType(int position) {
        int itemViewType = TYPE_BANNER;
        if (position > 0) {
            itemViewType = mList.get(position - 1).getObj_type();
        }
        return itemViewType;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (position == 0) {
            ret = mBanners;
        } else if (position > 0) {
            ret = mList.get(position - 1);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        switch (getItemViewType(position)) {
            case TYPE_BANNER:
                ret = setupBanners(convertView, parent);
                break;
            case TYPE_LIST_1_ITEM:
                ret = setupType1(position, convertView, parent);
                break;
            case TYPE_LIST_2_ITEM:
                ret = setupType2(position, convertView, parent);
                break;
            case TYPE_LIST_3_ITEM:
                ret = setupType3(position, convertView, parent);
                break;
        }
        return ret;
    }

    private View setupType3(int position, View convertView, ViewGroup parent) {
        View view = null;
        Type3Holder type3Holder;
        if (convertView != null) {
            view = convertView;
            type3Holder = (Type3Holder) view.getTag();
        } else {
            view = mLayoutInflater.inflate(R.layout.item3_lv_fragment_main_recommend_list, parent, false);
            type3Holder = new Type3Holder();
        }
        return view;
    }

    private View setupType2(int position, View convertView, ViewGroup parent) {
        View view;
        Type2Holder type2Holder;
        if (convertView != null) {
            view = convertView;
            type2Holder = (Type2Holder) view.getTag();
        } else {
            view = mLayoutInflater.inflate(R.layout.item2_lv_fragment_main_recommend_list, parent, false);
            type2Holder = new Type2Holder();
            view.setTag(type2Holder);
            type2Holder.tv_item2_lv_fragment_main_recommend_list_title = (TextView) view.findViewById(R.id.tv_item2_lv_fragment_main_recommend_list_title);
            /**/
            type2Holder.tv1_item_gv_item2_lv_fragment_main_recommend_grid = (TextView) view.findViewById(R.id.tv1_item_gv_item2_lv_fragment_main_recommend_grid);
            type2Holder.ll1_item_gv_item2_lv_fragment_main_recommend_grid = view.findViewById(R.id.ll1_item_gv_item2_lv_fragment_main_recommend_grid);
            type2Holder.iv1_item_gv_item2_lv_fragment_main_recommend_grid = (ImageView) view.findViewById(R.id.iv1_item_gv_item2_lv_fragment_main_recommend_grid);
            /**/
            type2Holder.tv2_item_gv_item2_lv_fragment_main_recommend_grid = (TextView) view.findViewById(R.id.tv2_item_gv_item2_lv_fragment_main_recommend_grid);
            type2Holder.ll2_item_gv_item2_lv_fragment_main_recommend_grid = view.findViewById(R.id.ll2_item_gv_item2_lv_fragment_main_recommend_grid);
            type2Holder.iv2_item_gv_item2_lv_fragment_main_recommend_grid = (ImageView) view.findViewById(R.id.iv2_item_gv_item2_lv_fragment_main_recommend_grid);
            /**/
            type2Holder.tv3_item_gv_item2_lv_fragment_main_recommend_grid = (TextView) view.findViewById(R.id.tv3_item_gv_item2_lv_fragment_main_recommend_grid);
            type2Holder.ll3_item_gv_item2_lv_fragment_main_recommend_grid = view.findViewById(R.id.ll3_item_gv_item2_lv_fragment_main_recommend_grid);
            type2Holder.iv3_item_gv_item2_lv_fragment_main_recommend_grid = (ImageView) view.findViewById(R.id.iv3_item_gv_item2_lv_fragment_main_recommend_grid);
        }
        Recommend.ListsEntity listsEntity = mList.get(position - 1);
        List<Recommend.ListsEntity.ComicsEntity> list = listsEntity.getComics();
        type2Holder.tv_item2_lv_fragment_main_recommend_list_title.setText(listsEntity.getName());
        if (list != null && list.size() >= 3) {
            Recommend.ListsEntity.ComicsEntity item0 = list.get(0);
            Recommend.ListsEntity.ComicsEntity item1 = list.get(1);
            Recommend.ListsEntity.ComicsEntity item2 = list.get(2);
            /**/
            type2Holder.tv1_item_gv_item2_lv_fragment_main_recommend_grid.setText(item0.getName());
            Picasso.with(mContext).load(item0.getCover_img()).fit().into(type2Holder.iv1_item_gv_item2_lv_fragment_main_recommend_grid);
            type2Holder.ll1_item_gv_item2_lv_fragment_main_recommend_grid.setTag(item0);
            type2Holder.ll1_item_gv_item2_lv_fragment_main_recommend_grid.setOnClickListener(this);
            /**/
            type2Holder.tv2_item_gv_item2_lv_fragment_main_recommend_grid.setText(item1.getName());
            Picasso.with(mContext).load(item1.getCover_img()).fit().into(type2Holder.iv2_item_gv_item2_lv_fragment_main_recommend_grid);
            type2Holder.ll2_item_gv_item2_lv_fragment_main_recommend_grid.setTag(item1);
            type2Holder.ll2_item_gv_item2_lv_fragment_main_recommend_grid.setOnClickListener(this);
            /**/
            type2Holder.tv3_item_gv_item2_lv_fragment_main_recommend_grid.setText(item2.getName());
            Picasso.with(mContext).load(item2.getCover_img()).fit().into(type2Holder.iv3_item_gv_item2_lv_fragment_main_recommend_grid);
            type2Holder.ll3_item_gv_item2_lv_fragment_main_recommend_grid.setTag(item2);
            type2Holder.ll3_item_gv_item2_lv_fragment_main_recommend_grid.setOnClickListener(this);
        }
        return view;
    }

    /**
     * 类型1的View
     */
    private View setupType1(int position, View convertView, ViewGroup parent) {
        View ret;
        final Type1Holder type1Holder;
        if (convertView != null) {
            ret = convertView;
            type1Holder = (Type1Holder) ret.getTag();
        } else {
            ret = mLayoutInflater.inflate(R.layout.item1_lv_fragment_main_recommend_list, parent, false);
            type1Holder = new Type1Holder();
            type1Holder.ll_item1_lv_fragment_main_recommend_list = (LinearLayout) ret.findViewById(R.id.ll_item1_lv_fragment_main_recommend_list);
            type1Holder.tv_item1_lv_fragment_main_recommend_list_title = (TextView) ret.findViewById(R.id.tv_item1_lv_fragment_main_recommend_list_title);
            type1Holder.rl_item1_lv_fragment_main_recommend_list_title = ret.findViewById(R.id.rl_item1_lv_fragment_main_recommend_list_title);
            ret.setTag(type1Holder);
        }
        type1Holder.ll_item1_lv_fragment_main_recommend_list.removeAllViews();
        Recommend.ListsEntity listsEntity = mList.get(position - 1);
        String title = listsEntity.getName();
        type1Holder.tv_item1_lv_fragment_main_recommend_list_title.setText(title);
        List<Recommend.ListsEntity.ThemeListEntity> theme_list = listsEntity.getTheme_list();
        int size = theme_list.size();
        if (size % 2 != 0) {
            theme_list.remove(theme_list.get(size - 1));
        }
        for (int i = 0; i < theme_list.size() / 2; i++) {
            Recommend.ListsEntity.ThemeListEntity item1 = theme_list.get(2 * i);
            Recommend.ListsEntity.ThemeListEntity item2 = theme_list.get(2 * i + 1);
            View item_grid = mLayoutInflater.inflate(R.layout.item_gv_item1_lv_fragment_main_recommend_grid, type1Holder.ll_item1_lv_fragment_main_recommend_list, false);
            View ll1 = item_grid.findViewById(R.id.ll1_item_gv_item1_lv_fragment_main_recommend_grid);
            View ll2 = item_grid.findViewById(R.id.ll2_item_gv_item1_lv_fragment_main_recommend_grid);
            ll1.setTag(item1);
            ll2.setTag(item2);
            ll1.setOnClickListener(this);
            ll2.setOnClickListener(this);
            ImageView iv1 = (ImageView) item_grid.findViewById(R.id.iv1_item_gv_item1_lv_fragment_main_recommend_grid);
            TextView tv1 = (TextView) item_grid.findViewById(R.id.tv1_item_gv_item1_lv_fragment_main_recommend_grid);
            ImageView iv2 = (ImageView) item_grid.findViewById(R.id.iv2_item_gv_item1_lv_fragment_main_recommend_grid);
            TextView tv2 = (TextView) item_grid.findViewById(R.id.tv2_item_gv_item1_lv_fragment_main_recommend_grid);
            tv1.setText(item1.getName());
            tv2.setText(item2.getName());
            Picasso.with(mContext).load(item1.getCover()).fit().placeholder(R.mipmap.ic_launcher).into(iv1);
            Picasso.with(mContext).load(item2.getCover()).fit().placeholder(R.mipmap.ic_launcher).into(iv2);
            type1Holder.ll_item1_lv_fragment_main_recommend_list.addView(item_grid);
        }
        return ret;
    }

    /**
     * 顶部banners
     */
    private View setupBanners(View convertView, ViewGroup parent) {
        View ret;
        final BannerViewHolder bannerHolder;
        if (convertView != null) {
            ret = convertView;
        } else {
            ret = mLayoutInflater.inflate(R.layout.item0_lv_fragment_main_recommend_banner, parent, false);
            bannerHolder = new BannerViewHolder();
            bannerHolder.vp_lv_fragment_main_recommend_banners = (AutoScrollViewPager) ret.findViewById(R.id.vp_lv_fragment_main_recommend_banners);
            bannerHolder.tv_lv_fragment_main_recommend_banners = (TextView) ret.findViewById(R.id.tv_lv_fragment_main_recommend_banners);
            bannerHolder.rg_lv_fragment_main_recommend_banners = (RadioGroup) ret.findViewById(R.id.rg_lv_fragment_main_recommend_banners);
            ret.setTag(bannerHolder);
            final RecommendBannerPagerAdapter adapter = new RecommendBannerPagerAdapter(mContext, mBanners, this);
            bannerHolder.vp_lv_fragment_main_recommend_banners.setAdapter(adapter);
            bannerHolder.vp_lv_fragment_main_recommend_banners.setStopScrollWhenTouch(true);
            bannerHolder.vp_lv_fragment_main_recommend_banners.setEnabled(false);
            bannerHolder.vp_lv_fragment_main_recommend_banners.setSwipeScrollDurationFactor(5);
            bannerHolder.vp_lv_fragment_main_recommend_banners.setBorderAnimation(false);
            bannerHolder.vp_lv_fragment_main_recommend_banners.startAutoScroll(1500);
            bannerHolder.tv_lv_fragment_main_recommend_banners.setText(adapter.getItem(0).getContent());
            for (int i = 0; i < adapter.getRealCount(); i++) {
                RadioButton radioButton = new RadioButton(mContext);
                radioButton.setId(i);
                bannerHolder.rg_lv_fragment_main_recommend_banners.addView(radioButton);
            }
            bannerHolder.rg_lv_fragment_main_recommend_banners.check(0);
            bannerHolder.vp_lv_fragment_main_recommend_banners.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    int realPosition = position % adapter.getRealCount();
                    Recommend.BannersEntity item = adapter.getItem(position);
                    bannerHolder.tv_lv_fragment_main_recommend_banners.setText(item.getContent());
                    bannerHolder.rg_lv_fragment_main_recommend_banners.check(realPosition);
                }
            });
        }
        return ret;
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag instanceof Recommend.ListsEntity.ThemeListEntity) {
            Recommend.ListsEntity.ThemeListEntity item = (Recommend.ListsEntity.ThemeListEntity) tag;
            Toast.makeText(mContext, item.getName(), Toast.LENGTH_SHORT).show();
        } else if (tag instanceof Recommend.BannersEntity) {
            Recommend.BannersEntity entity = (Recommend.BannersEntity) tag;
            Toast.makeText(mContext, entity.getContent(), Toast.LENGTH_SHORT).show();
        } else if (tag instanceof Recommend.ListsEntity.ComicsEntity) {
            Recommend.ListsEntity.ComicsEntity entity = (Recommend.ListsEntity.ComicsEntity) tag;
            Toast.makeText(mContext, entity.getName(), Toast.LENGTH_SHORT).show();
        }
    }

    private static class BannerViewHolder {
        public AutoScrollViewPager vp_lv_fragment_main_recommend_banners;
        public TextView tv_lv_fragment_main_recommend_banners;
        public RadioGroup rg_lv_fragment_main_recommend_banners;
    }

    private static class Type1Holder {
        private TextView tv_item1_lv_fragment_main_recommend_list_title;
        private View rl_item1_lv_fragment_main_recommend_list_title;
        private LinearLayout ll_item1_lv_fragment_main_recommend_list;
    }

    private static class Type2Holder {
        private TextView tv_item2_lv_fragment_main_recommend_list_title;
        private View ll1_item_gv_item2_lv_fragment_main_recommend_grid;
        private ImageView iv1_item_gv_item2_lv_fragment_main_recommend_grid;
        private TextView tv1_item_gv_item2_lv_fragment_main_recommend_grid;
        private View ll2_item_gv_item2_lv_fragment_main_recommend_grid;
        private ImageView iv2_item_gv_item2_lv_fragment_main_recommend_grid;
        private TextView tv2_item_gv_item2_lv_fragment_main_recommend_grid;
        private View ll3_item_gv_item2_lv_fragment_main_recommend_grid;
        private ImageView iv3_item_gv_item2_lv_fragment_main_recommend_grid;
        private TextView tv3_item_gv_item2_lv_fragment_main_recommend_grid;
    }

    private static class Type3Holder {
        private TextView tv_item1_lv_fragment_main_recommend_list_title;
        private View rl_item1_lv_fragment_main_recommend_list_title;
        private GridView gv_item1_lv_fragment_main_recommend_list;
    }
}
